package com.samuel.blog.service.impl;

import com.samuel.blog.constant.CommonConstant;
import com.samuel.blog.dao.AuthorDao;
import com.samuel.blog.dao.JurisdictionDao;
import com.samuel.blog.dao.RoleDao;
import com.samuel.blog.entity.ArticleEntity;
import com.samuel.blog.entity.AuthorEntity;
import com.samuel.blog.entity.JurisdictionEntity;
import com.samuel.blog.entity.RoleEntity;
import com.samuel.blog.service.ArticleService;
import com.samuel.blog.service.AuthorService;
import com.samuel.blog.utils.RedisUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kaiji
 */
@Transactional
@Service("authorService")
public class AuthorServiceImpl extends BaseServiceImpl<AuthorEntity, String> implements AuthorService {
	private static final Logger logger = Logger.getLogger(AuthorServiceImpl.class);
	
	@Resource(name = "authorDao")
	private AuthorDao authorDao;
	@Resource(name = "articleService")
	ArticleService articleService;
	@Resource(name = "redisUtil")
	private RedisUtil redisUtil;
	@Resource(name = "roleDao")
	private RoleDao roleDao;
	@Resource(name = "jurisdictionDao")
	private JurisdictionDao jurisdictionDao;
	
	
	
	@Override
	public AuthorEntity getByPhone(String phone) {
		Map<String, Object> authorMap = new HashMap<>();
		authorMap.put("phone", phone);
		List<AuthorEntity> selectByMap = authorDao.selectByMap(authorMap);
		return null == selectByMap || selectByMap.size() <= 0 ? null : selectByMap.get(0);
	}

	@Override
	public AuthorEntity getByRealName(String realName) {
		Map<String, Object> authorMap = new HashMap<>();
		authorMap.put("realName", realName);
		List<AuthorEntity> selectByMap = authorDao.selectByMap(authorMap);
		return null == selectByMap || selectByMap.size() <= 0 ? null : selectByMap.get(0);
	}

	@Override
	public AuthorEntity getByUserName(String userName) {
		Map<String, Object> authorMap = new HashMap<>();
		authorMap.put("userName", userName);
		List<AuthorEntity> selectByMap = authorDao.selectByMap(authorMap);
		return null == selectByMap || selectByMap.size() <= 0 ? null : selectByMap.get(0);
	}

	@Override
	public boolean deleteBatchByIds(String[] authorIds) {
		try {
			for (String authorId : authorIds) {
				// 关联的文章 也要被修改
				List<ArticleEntity> articleList = articleService.getByAuthorId(authorId);
				if (null != articleList && articleList.size() > 0) {
					for (ArticleEntity articleEntity : articleList) {
						articleEntity.setDeleteStatus(CommonConstant.STOP_USE);
					}
					articleService.updateBatchById(articleList);
				}
				AuthorEntity author = authorDao.selectByPrimaryKey(authorId);
				if (null != author) {
					author.setDeleteStatus(CommonConstant.STOP_USE);
					authorDao.updateByPrimaryKeySelective(author);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("作者联级删除错误异常: " + e.getMessage());
			return false;
		}
		return true;
	}

	@Override
	public int saveAuthorRole(String authorId, String roleId) {
		try {
			// 先删除
			authorDao.deleteAuthorRole(authorId);
			Map<String, Object> map = new HashMap<>();
			map.put("authorId", authorId);
			map.put("roleId", roleId);
			map.put("createDate", new Date());
			authorDao.saveAuthorRole(map);
			return 1;
			
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public void saveRedis(AuthorEntity author) {
		// 更新作者的登录信息
		authorDao.updateByPrimaryKeySelective(author);
		RoleEntity role = roleDao.selectByAuthorId(author.getAuthorId());
		if (null != role) {
			redisUtil.put("author_" + author.getAuthorId(), role);
			List<JurisdictionEntity> selectByRoleId = jurisdictionDao.selectByRoleId(role.getRoleId());
			if (null != selectByRoleId && selectByRoleId.size() > 0) {
				redisUtil.putList("role_" + author.getAuthorId(), selectByRoleId);
			}
		}
	}

	@Override
	public RoleEntity selectRoleByRedis(String authorId) {
		return redisUtil.get("author_" + authorId, RoleEntity.class);
	}

	@Override
	public List<JurisdictionEntity> selectJurByRedis(String authorId) {
		return redisUtil.getList("role_" +  authorId, JurisdictionEntity.class);
	}

}
